package com.example.demo.config;

import com.example.demo.domain.enums.OrderStatus;
import com.example.demo.domain.enums.OrderStatusChangeEvent;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.persist.StateMachinePersister;

@Configuration
public class StateMachinePersisterConfig {


    @Autowired
    private BizOrderStateMachineContextPersist bizOrderRedisStateMachineContextPersist;

    @Bean(name = "bizOrderStateMachineContextPersister",autowire = Autowire.BY_TYPE)
    public StateMachinePersister<OrderStatus, OrderStatusChangeEvent,String> bizOrderRedisStateMachinePersister() {
        return new BizOrderStateMachineContextPersister(bizOrderRedisStateMachineContextPersist);
    }


    /**
     * 内存持久化配置
     */
   /* @Bean
    public DefaultStateMachinePersister persister() {
        return new DefaultStateMachinePersister(new StateMachinePersist() {
            //用户保存所有状态机上下文
            private Map map = new HashMap();

            // 持久化状态机
            @Override
            public void write(StateMachineContext context, Object contextObj) throws Exception {
                System.out.println("持久化状态机：contextObj：" + contextObj.toString());
                map.put(contextObj, context);
            }

            // 获取状态机
            @Override
            public StateMachineContext read(Object contextObj) throws Exception {
                System.out.println("获取状态机，contextObj：" + contextObj.toString());
                StateMachineContext stateMachineContext = (StateMachineContext) map.get(contextObj);
                return stateMachineContext;
            }
        });
    }*/
}
